<?php
/**
 * Created by PhpStorm.
 * User: krystal
 * Date: 2019/6/17
 * Time: 11:49
 */

namespace app\api\controller\v20;


use app\api\controller\v13\Auth2;
use app\common\cachemodel\AppConfig;
use app\common\cachemodel\MallGoods;
use app\common\cachemodel\MallGoodsAttr;
use app\common\cachemodel\MallGoodsTags;
use app\common\cachemodel\MallRecommendGoods;
use app\common\lib\Util;

/**
 * @version 2.3.0
 * Class PhysicalStore
 * @package app\api\controller\v20
 */
class MallStore extends Auth2
{
    /**
     * 实体商品列表
     * @return \think\response\Json
     * @throws \Exception
     */
    public function goods_list()
    {
        $goods = (new MallGoods())->getGoodsPage(0, ['id', 'name', 'imgs', 'now_price', 'vip_price'], 10);

        if ($this->is_apple_check)
        {
            $notDisplayGoodsConfig = (new AppConfig())->configInfo('apple_check');
            $notDisplayGids = isset($notDisplayGoodsConfig['mall_not_display_goods']) && !empty($notDisplayGoodsConfig['mall_not_display_goods']) ? explode(',', $notDisplayGoodsConfig['mall_not_display_goods']) : [];

            $goods['ids'] = array_diff($goods['ids'], $notDisplayGids);
        }

        $goods['data'] = $this->resetList($goods['ids'], $goods['data']);
        unset($goods['ids']);
        return show(config('code.success'), '获取成功', $goods);
    }

    /**
     * 商品列表数据重组
     * @param $ids
     * @param $data
     * @return array
     */
    private function resetList($ids, $data)
    {
        $arr = [];
        foreach ($ids as $id)
        {
            $img = $data[$id]['imgs'] ? explode(',', $data[$id]['imgs'])[0] : '';
            if ($data[$id]['vip_price'] > 0)
            {   // 设置了会员价显示
                $vipPrice = $data[$id]['vip_price'];
            } else
            {   // 没有设置会员价显示
                $vipPrice = null;
            }


            $arr[] = [
                'gid' => (int)$data[$id]['id'],
                'img' => $img ? $img . '?x-oss-process=style/scale0.2' : '',
                'title' => $data[$id]['name'],
                'vip_desc' => '行星会员价',
                'vip_price' => (string)$vipPrice,
                'price' => (string)$data[$id]['now_price'],
            ];
        }
        return $arr;
    }

    /**
     * 实体商品详情接口
     * @return \think\response\Json
     * @throws \Exception
     */
    public function goods_detail()
    {
        $gid = input('gid/d', 0);
        $mallGoodsModel = new MallGoods();
        $goods = current($mallGoodsModel->batGetInfo([$gid], ['name', 'imgs', 'now_price', 'vip_price', 'status']));//获取当前商品的信息
        if ($goods)
        {
            $status = $goods['status'] ? 1 : 0;
            $goodsContent = $mallGoodsModel->getGoodsContent($gid);//获取商品图文信息

            $spectInfo = (new MallGoodsAttr())->getGoodsAttrList($gid, ['name', 'id', 'now_price', 'vip_price', 'inventory', 'img']);//获取商品的规格信息

            $tags = (new MallGoodsTags())->getGoodsTags($gid, ['name']);//获取商品的标签信息
            $tags = $tags ? array_column($tags, 'name') : [];

            $allGoodsIds = $mallGoodsModel->getGoodsList();//获取所有的商品id
            $newGoodsIds = [];
            if ($allGoodsIds)
            {
                $notDisplayGids = [];
                if ($this->is_apple_check)
                {   // 苹果审核禁止 显示商品
                    $notDisplayGoodsConfig = (new AppConfig())->configInfo('apple_check');
                    $notDisplayGids = isset($notDisplayGoodsConfig['mall_not_display_goods']) && !empty($notDisplayGoodsConfig['mall_not_display_goods']) ? explode(',', $notDisplayGoodsConfig['mall_not_display_goods']) : [];
                }

                $notDisplayGids[] = $gid;
                $newGoodsIds = array_diff($allGoodsIds, $notDisplayGids);
            }
            $newGoodsInfo = $mallGoodsModel->batGetInfo(array_slice($newGoodsIds, 0, 6), ['id', 'imgs', 'name', 'now_price']);//获取不包含当前商品的六个商品信息
            //重组推荐商品数据格式
            $recommendGoods = [];
            foreach ($newGoodsInfo as $k => $good)
            {
                $img = $good['imgs'] ? explode(',', $good['imgs'])[0] : '';
                $recommendGoods[] = [
                    'img' => $img ? $img . '?x-oss-process=style/scale0.2' : '',
                    'gid' => (int)$good['id'],
                    'title' => $good['name'],
                    'price' => $good['now_price']
                ];
            }

            if ($goods['vip_price'] > 0)
            {   // 设置了会员价显示
                $vipPrice = (string)$goods['vip_price'];
            } else
            {   // 没有设置会员价显示
                $vipPrice = null;
            }
            $arr = [
                'img_list' => $goods['imgs'] ? explode(',', $goods['imgs']) : [],
                'title' => $goods['name'],
                'content' => $goodsContent,
                'price' => (string)$goods['now_price'],
                'vip_price' => $vipPrice,
                'vip_desc' => '行星会员价',
                'tag_list' => $tags,
                'status' => $status,
                'recommend_goods' => $recommendGoods,
                'recommend_title' => '「万幸」精选好物'
            ];

            $spect_list = [];
            foreach ($spectInfo as $item)
            {
                $spect_list[] = [
                    'title' => $item['name'],
                    'price' => $item['now_price'],
                    'vip_price' => $item['vip_price'],
                    'num' => (int)($status ? $item['inventory'] : 0),
                    'img' => $item['img'],
                    'id' => (int)$item['id']
                ];
            }
            $arr['spect_list'] = $spect_list;
            return show(config('code.success'), '获取数据成功', $arr);
        }
        return show(config('code.error'), '商品不存在');
    }

    /**
     * 获取不同类型的推荐商品信息
     * @throws \Exception
     */
    public function recommend_goods()
    {
        $type = input('type/d', 0);
        if(Util::AppIsPassShowV1())
        {
            return show(config('code.success'), '获取成功');
        }
        $data = (new MallRecommendGoods())->getGoodsTypeRecommend($type);

        if (!$data['title'] && !$data['banner_data'] && !$data['good_list'])
        {
            $data = null;
        }
        if ($this->is_apple_check && isset($data['good_list']) && !empty($data['good_list']))
        {   // 苹果审核禁止 显示商品
            $notDisplayGoodsConfig = (new AppConfig())->configInfo('apple_check');
            $notDisplayGids = isset($notDisplayGoodsConfig['mall_not_display_goods']) && !empty($notDisplayGoodsConfig['mall_not_display_goods']) ? explode(',', $notDisplayGoodsConfig['mall_not_display_goods']) : [];

            $goods = [];
            foreach ($data['good_list'] as $goodInfo)
            {
                if (!in_array($goodInfo['gid'], $notDisplayGids, false))
                {
                    $goods[] = $goodInfo;
                }
            }

            $data['good_list'] = $goods;
        }


        return show(config('code.success'), '获取成功', $data);
    }
}
